<script setup>
import {
    Management,
    Promotion,
    UserFilled,
    User,
    Crop,
    EditPen,
    SwitchButton,
    CaretBottom
} from '@element-plus/icons-vue'
import avatar from '@/assets/default.png'

import {userInfoService} from '@/api/user.js'
import useUserInfoStore from '@/stores/userInfo.js'
import {useTokenStore} from '@/stores/token.js'
const tokenStore = useTokenStore();
const userInfoStore = useUserInfoStore();

//调用函数,获取用户详细信息
const getUserInfo = async()=>{
    //调用接口
    // let result = await userInfoService();
    // //数据存储到pinia中
    // userInfoStore.setInfo(result.data);
}

// localStorage.setItem('role', 'user');
const role_data = localStorage.getItem('role');
const img_data = localStorage.getItem('img');
console.log(img_data);

console.log(role_data)

getUserInfo();
//条目被点击后,调用的函数
import {useRouter} from 'vue-router'
const router = useRouter();
import {ElMessage,ElMessageBox} from 'element-plus'
const handleCommand = (command)=>{
    //判断指令
    if(command === 'logout'){
        //退出登录
        ElMessageBox.confirm(
        '您确认要退出吗?',
        '温馨提示',
        {
            confirmButtonText: '确认',
            cancelButtonText: '取消',
            type: 'warning',
        }
    )
        .then(async () => {
            //退出登录
            //1.清空pinia中存储的token以及个人信息
            tokenStore.removeToken()
            userInfoStore.removeInfo()
            localStorage.removeItem('role');
            localStorage.removeItem('token');
            localStorage.removeItem('userId');

            //2.跳转到登录页面
            router.push('/login')
            ElMessage({
                type: 'success',
                message: '退出登录成功',
            })
            
        })
        .catch(() => {
            ElMessage({
                type: 'info',
                message: '用户取消了退出登录',
            })
        })
    }else{
        //路由
        router.push('/user/'+command)
    }
}



const lanjie = ()=>{
    //判断是否登录
    if(role_data){
        //已登录
        return true;
    }else{
        //未登录
        router.push('/login')
    }
}
lanjie();

</script>

<template>
    <!-- element-plus中的容器 -->
    <el-container class="layout-container">
        <!-- 左侧菜单 -->
        <el-aside width="200px">
            <div class="el-aside__logo" @click="router.push('/')"> <h4 style="color: #606266;display: flex;justify-content: center;">小区维修系统管理平台</h4> </div>
            <!-- element-plus的菜单标签 -->
            <el-menu active-text-color="#252225" background-color="#ebf1f5"  text-color="#fff"
                router>
                <el-menu-item index="/personal">
                    <el-icon>
                        <Management />
                    </el-icon>
                    <span>个人中心</span>
                </el-menu-item>
                <el-menu-item index="/gonggao" v-if="role_data == 'admin'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>社区公告</span>
                </el-menu-item>
                <el-sub-menu  v-if="role_data == 'admin'">
                    <template #title>
                        <el-icon>
                            <UserFilled />
                        </el-icon>
                        <span style="color: #606266;">住户管理</span>
                    </template>
                    <el-menu-item index="userList">
                        <el-icon>
                            <User />
                        </el-icon>
                        <span>住户管理</span>
                    </el-menu-item>
                    <el-menu-item index="/adminWeixiu">
                        <el-icon>
                            <Crop />
                        </el-icon>
                        <span>住户维修记录</span>
                    </el-menu-item>
                    <el-menu-item index="/adminJubao">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>住户举报记录</span>
                    </el-menu-item>
                    <el-menu-item index="/adminLiuyan">
                        <el-icon>
                            <EditPen />
                        </el-icon>
                        <span>住户留言</span>
                    </el-menu-item>
                </el-sub-menu>
                <el-menu-item index="/adminUserweixiu"  v-if="role_data == 'admin'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>维修人员管理</span>
                </el-menu-item>
                <el-menu-item index="/adminweixiuList" v-if="role_data == 'admin'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>待处理维修</span>
                </el-menu-item>
                <el-menu-item index="/adminOrder" v-if="role_data == 'admin'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>订单信息管理</span>
                </el-menu-item>
                <el-menu-item index="/adminWeixiuType" v-if="role_data == 'admin'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>维修种类</span>
                </el-menu-item>


                <el-menu-item index="/workerOrder" v-if="role_data == 'workers'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>历史维修订单</span>
                </el-menu-item>
                <el-menu-item index="/workerJiedan" v-if="role_data == 'workers'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>未完成订单</span>
                </el-menu-item>
                <el-menu-item index="/workerJubao" v-if="role_data == 'workers'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>举报信息</span>
                </el-menu-item>


                <el-menu-item index="/userOrderList" v-if="role_data == 'user'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>维修订单</span>
                </el-menu-item>
                <el-menu-item index="/userJiedan" v-if="role_data == 'user'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>维修申请</span>
                </el-menu-item>
                <el-menu-item index="/userJubao" v-if="role_data == 'user'">
                    <el-icon>
                        <Promotion />
                    </el-icon>
                    <span>投诉信息</span>
                </el-menu-item>
            </el-menu>
        </el-aside>
        <!-- 右侧主区域 -->
        <el-container>
            <!-- 头部区域 -->
            <el-header>
                <div>小区维修系统：<strong>{{ userInfoStore.info.nickname }}</strong></div>
                <!-- 下拉菜单 -->
                <!-- command: 条目被点击后会触发,在事件函数上可以声明一个参数,接收条目对应的指令 -->
                <el-dropdown placement="bottom-end" @command="handleCommand">
                    <span class="el-dropdown__box">
                        <el-avatar :src="img_data" />
                        <el-icon>
                            <CaretBottom />
                        </el-icon>
                    </span>
                    <template #dropdown>
                        <el-dropdown-menu>
                            <!-- <el-dropdown-item command="personal" :icon="User">基本资料</el-dropdown-item> -->
                            <el-dropdown-item command="logout" :icon="SwitchButton">退出登录</el-dropdown-item>
                        </el-dropdown-menu>
                    </template>
                </el-dropdown>
            </el-header>
            <!-- 中间区域 -->
            <el-main>
                <router-view></router-view>
            </el-main>
            <!-- 底部区域 -->
        </el-container>
    </el-container>
</template>

<style lang="scss" scoped>
.layout-container {
    height: 100vh;

    .el-aside {
        background-color: #ebf1f5;

        .el-menu {
            border-right: none;

            .el-menu-item.is-active,
            .el-sub-menu.is-active {
                color: #409EFF;  // 蓝色字体
                background-color: #fff;  // 白色背景
            }

            .el-menu-item,
            .el-sub-menu__title {
                color: #606266;  // 设置菜单项字体颜色为黑色
            }
            .el-icon {
                color: #606266;  // 默认图标颜色为黑色
            }
        }
    }

    .el-header {
        background-color: #fff;
        display: flex;
        align-items: center;
        justify-content: space-between;

        .el-dropdown__box {
            display: flex;
            align-items: center;

            .el-icon {
                color: #606266;  // 设置图标颜色为黑色
                margin-left: 10px;
            }

            .el-avatar {
                border: 2px solid #606266; // 设置头像的边框为黑色（可选）
            }

            &:active,
            &:focus {
                outline: none;
            }
        }

        /* 设置头部区域的字体颜色为黑色 */
        color: #000;
    }

    .el-footer {
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 14px;
        color: #ecd0d0; // 可以根据需要调整底部区域字体颜色
    }
}
</style>
